package com.aote.entity.component;

import org.w3c.dom.Element;

import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.ReplacableParameterComponent;

public class LinkedTableSearchButton extends ReplacableParameterComponent {

	public LinkedTableSearchButton(HTMLBasicComponent parent) throws Exception {
		super(parent);
	}

	/**
	 * 将onclick中的＃patentid＃替换为要引用数据的table所对应的parentid
	 */
	public void updateElement() {
		// 本按钮所在组件对应的实体的parentid
		Entity entity = (Entity) this.getParent().getModel();
		String parentid = (String) entity.getAttributes().get("parentid");
		// 从元素中取出要找table的父组件所对应的实体类型
		Element element = this.getElement();
		String parentEntityType = element.getAttribute("parententitytype");
		// table的父组件所对应的entity的ID
		Entity ent = (Entity) EntityManager.getInstance().getEntities(
				parentEntityType, "parentid", parentid).getFirst();
		String tableParentID = ent.getID();
		// 将onclick中的#parentid#换掉
		String str = element.getAttribute("onclick");
		str = str.replaceAll("#parentid#", tableParentID);
		element.setAttribute("onclick", str);
	}
}
